package com.chenfan.finance.exception;

import com.chenfan.finance.commons.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * 全局异常捕捉  FinanceTipException
 *
 * @author: xuxianbei
 * Date: 2020/8/31
 * Time: 17:50
 * Version:V1.0
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(value = {Exception.class, IllegalArgumentException.class})
    public Response<String> returnErrorCode(Throwable ex) {
        log.info(getStackTrace(ex));
        return new Response(500, ex.getMessage());
    }

    public static String getStackTrace(Throwable throwable) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);

        String var3;
        try {
            throwable.printStackTrace(pw);
            var3 = sw.toString();
        } finally {
            pw.close();
        }

        return var3;
    }
}
